
#include "SdbOpt.h"
#include <loong/Utils.h>

#include <loong/Logger.h>

SdbOpt::~SdbOpt()
{
    m_sdbConn.disconnect();
}

bool SdbOpt::initSdb(string &addrList, int &port, string &userName, string &passwd)
{
	int32_t rc = SDB_OK;
	bool ret = true;
	vector<std::string> addrs = loong::parseDotString(addrList);

	//char **addr = new char*[23];
	char **addr = new char*[addrs.size()];

	for (uint32_t i = 0; i < addrs.size(); i++)
	{
		addr[i] = new char[23];
		strncpy(addr[i], addrs.at(i).data(), 23);
	}

	// connect to database
	rc = m_sdbConn.connect((const char**)addr, 3, userName.c_str(), passwd.c_str());
	if (rc != SDB_OK)
	{
		ERROR("Fail to connet to database, rc = %d", rc);
		m_sdbConn.disconnect();
		ret = false;
	}
	else
	{
		INFO("Connect sequoiadb success!");
	}

	for (uint32_t i = 0; i < addrs.size(); i++)
	{
		addr[i] = new char[23];
		delete[] addr[i];
	}
	delete[] addr;

	return ret;
}


